using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimatorTrigger : MonoBehaviour
{

    private  PlayerStateController mPlayerStateController;
    
    private void Awake()
    {
        mPlayerStateController = GetComponentInParent<PlayerStateController>();
    }

    public void TriggerFinish()
    {
        mPlayerStateController.stateMachine.TriggerFinish();
    }
    
    public void TriggerAttack()
    {
        Collider2D[] collider2Ds = Physics2D.OverlapCircleAll(mPlayerStateController.GetAttackCheckPos(),
            Setting.attackRadius,Setting.EnemyLayer);
        
        foreach (Collider2D item in collider2Ds)
        {
            EnemyStateController enemyStateController = item.GetComponent<EnemyStateController>();
            if (enemyStateController != null)
            {
                //TODO:伤害计算
                enemyStateController.Damage(new DamgeInfo()
                {
                    damage = mPlayerStateController.GetDamage(),
                    attacker = mPlayerStateController.characterBase,
                    targeter = enemyStateController.characterBase,
                    hitBackForce = new  Vector2(mPlayerStateController.faceDir* 5, 5),
                    hitBackTime = 0.2f
                });
            }
        }
    }
    
    public void TriggerAttackCancel()
    {
        if (mPlayerStateController.stateMachine.EqualCurState(Setting.primaryAttack))
        {
            mPlayerStateController.stateMachine.TriggerCancel();
        }
    }

    public void TriggerThrowSword()
    {
        //ThrowSowrd(Setting.SkillThrowSword);
        ThrowSowrd(Setting.curSwordSkill);
        //ThrowSowrd(Setting.SkillPierceSword);
    }

    public void TriggerDead()
    {
        mPlayerStateController.gameObject.SetActive(false);
        //TODO:弹出结束ui
        //暂停游戏
        Time.timeScale = 0;
    }
    
    private void ThrowSowrd(string skillSword)
    {
        SkillManager.Instance.CreateClone<ThrowSwordSkill>
        ("CloneSword",mPlayerStateController.transform.position
            ,mPlayerStateController.faceDir,skillSword);
    }
}
